<extend name="Public:mainbase" />
<block name="content">
	<style>
		#url_list{height: 300px;overflow-y: scroll}
	</style>
<script language="javascript" src="__PUBLIC__/js/member.js?{:time()}"></script>
	<link href="__PUBLIC__/css/selectbox.css" type="text/css" rel="stylesheet"/>
	<script src="__PUBLIC__/js/changtopy.js"></script>
	<script src="__PUBLIC__/js/getpyszm.js"></script>
	<script src="__PUBLIC__/js/jquery.singleselect.js"></script>
<!--  内容列表   -->
<form name="form2" method="post" action="">
<table width="98%" class="table table-bordered  table-hover table-condensed "  align="center">
<tr bgcolor="#E7E7E7" >
	<td height="28" colspan="11" background="__PUBLIC__/images/tbg.gif" style="padding-left:10px;">
	◆ 频道列表 &nbsp;&nbsp;&nbsp;&nbsp;<button type="button" class="btn btn-xs btn-warning" data-toggle="modal" data-target=".newchannelbox">添加频道</button>
	</td>
</tr>
<tr align="center" bgcolor="#FBFCE2" height="25">
	<td width="6%">ID</td>
	<td width="4%">选择</td>
	<td width="28%">名称</td>
	<td width="28%">cmsID</td>
	<td width="10%">排序</td>
	<td width="10%">状态</td>
	<td width="10%">操作</td>
</tr>
<foreach name="channelList" item="val">
<tr align='center' bgcolor="#FFFFFF" height="26">
	<td nowrap>{$val.chid}</td>
	<td>
		<input name="chid" type="checkbox" value="{$val.chid}" class="np" />
	</td>
	<td>{$val.name}</td>
	<td>{$val.cmsid}</td>
	<td>{$val.sort}</td>
	<td><eq name="val.status" value="1">启用<else />不启用</eq></td>
	<td>
		<a href="javascript:;" data-chid='{$val.chid}' class='editchannel'>修改</a>
		|
		<a href="javascript:;" data-chid='{$val.chid}' class='workflow'>审核流设置</a>
		|
		<a href="javascript:;" data-chid='{$val.chid}' class='delchannel'>删除</a>
	</td>
</tr>
</foreach>
<tr bgcolor="#ffffff">
<td height="36" colspan="11">
	&nbsp;
	<a href="javascript:selAll()" class="btn btn-xs btn-info"><i class='glyphicon glyphicon-ok'></i> 全选</a>
	<a href="javascript:noSelAll()" class="btn btn-xs btn-info"><i class='glyphicon glyphicon-remove'></i> 取消</a>
	<!-- <a href="javascript:checkArc(0)" class="btn btn-xs btn-info">&nbsp;审核&nbsp;</a> -->
	<a href="javascript:;" id="delchannelall" class="btn btn-xs btn-info"><i class="glyphicon glyphicon-trash"></i> 删除</a>
</td>
</tr>
</form>
<tr align="right" bgcolor="#F9FCEF">
	<td height="36" colspan="11" align="center">
		<div class='page'>{$showpage}</div>
	</td>
</tr>
</table>

<!--  搜索表单  -->
<form name='form3' action='{:U('Admin/User/index')}' method='get'>
<input type='hidden' name='dopost' value='listArchives' />
<table width='98%'  class='table' align="center" style="margin-top:8px">
  <tr bgcolor='#EEF4EA'>
    <td background='__PUBLIC__/images/wbg.gif' align='center'>
      <table border='0' cellpadding='0' cellspacing='0' height="32">
        <td nowrap>频道名称：</td>
        <td width='130'>
          	<input type='text' name='name' value='' style='width:120px' />
        </td>
       <td>
          <input name="imageField" type="image" src="__PUBLIC__/images/button_search.gif" width="60" height="22" border="0" class="np" />
       </td>
      </tr>
     </table>
   </td>
  </tr>
</table>
</form>


<!-- 添加职位弹窗 -->
<div class="modal fade newchannelbox" tabindex="-1" role="dialog" aria-labelledby="add_user_jump" id='newchannelbox'>
  <div class="modal-dialog modal-sm" style="width:40%">
  		
    <div class="modal-content">
		<div class="modal-header">
	        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	        <h4 class="modal-title" id="myModalLabel">添加频道信息</h4>
	    </div>
		<form action="" method="POST" class="form-horizontal" style="padding:15px 0">
		<div class="form-group">
			<label for="username" class="col-sm-3 control-label">频道名称</label>
			<div class="col-sm-5">
				<input name="name" type="text" placeholder="请输入频道名称" value="" class="form-control" value="">
			</div>
		</div>
		<div class="form-group">
			<label for="username" class="col-sm-3 control-label">CMS栏目ID</label>
			<div class="col-sm-5">
				<input name="cmsid" type="text" placeholder="CMS栏目ID" value="" class="form-control" value="">
			</div>
		</div>
        <div class="form-group">
	        <label for="password" class="col-sm-3 control-label">状态</label>
	        <div class="col-sm-5">
        		<label><input type="radio" name="status" value="1" checked="checked" /> 启用</label>
	        	<label><input type="radio" name="status" value="0" /> 不启用</label>
			</div>
        </div>

		<div class="form-group">
			<label for="realname" class="col-sm-3 control-label">排序</label>
			<div class="col-sm-5">
				<input name="sort" type="text" placeholder="排序" value="0" class="form-control">
			</div>
		</div>

		<div class="add-error-message error item"></div>

		<div class="form-group">
		    <div class="col-sm-offset-3 col-sm-10">
		      <button type="button" class="btn btn-success" id="newchannelBtn">确定添加</button>
		      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		      <button type="button" class="btn btn-danger"  data-dismiss="modal">取消</button>
		    </div>
		  </div>
		</form>
    </div>
  </div>
</div>


<!-- 添加用户弹窗 -->
<div class="modal fade editchannelbox" tabindex="-1" role="dialog" aria-labelledby="edit_user_jump" id='editchannelbox'>
  <div class="modal-dialog modal-sm" style="width:40%">
  		
    <div class="modal-content">
		<div class="modal-header">
	        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	        <h4 class="modal-title" id="myModalLabel">修改该职位信息</h4>
	    </div>
		<form action="" method="POST" class="form-horizontal" style="padding:15px 0">
		<div class="form-group">
			<label for="username" class="col-sm-3 control-label">频道名称</label>
			<div class="col-sm-5">
				<input name="name" type="text" placeholder="请输入频道名称" value="" class="form-control" value="">
			</div>
		</div>
		<div class="form-group">
			<label for="username" class="col-sm-3 control-label">CMS栏目ID</label>
			<div class="col-sm-5">
				<input name="cmsid" type="text" placeholder="CMS栏目ID" value="" class="form-control" value="">
			</div>
		</div>

        <div class="form-group">
	        <label for="password" class="col-sm-3 control-label">状态</label>
	        <div class="col-sm-5">
        		<label><input type="radio" name="status" id="status_1" value="1" /> 启用</label>
	        	<label><input type="radio" name="status" id="status_0" value="0" /> 不启用</label>
			</div>
        </div>

		<div class="form-group">
			<label for="realname" class="col-sm-3 control-label">排序</label>
			<div class="col-sm-5">
				<input name="sort" type="text" placeholder="排序" value="0" class="form-control">
			</div>
		</div>

		<div class="add-error-message error item"></div>

		<div class="form-group">
		    <div class="col-sm-offset-3 col-sm-10">
		      <input type="hidden" name="chid" value="0" />
		      <button type="submit" class="btn btn-success" id="editchannelBtn">确定修改</button>
		      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		      <button type="button" class="btn btn-danger"  data-dismiss="modal">取消</button>
		    </div>
		  </div>
		</form>
    </div>
  </div>
</div>

<div class="modal fade editworkflowbox" tabindex="-1" role="dialog" aria-labelledby="edit_user_jump" id='editworkflowbox'>
  <div class="modal-dialog modal-sm" style="width:40%">
  		
    <div class="modal-content">
		<div class="modal-header">
	        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	        <h4 class="modal-title" id="myModalLabel">工作流设置</h4>
	    </div>
		<form action="" method="POST" class="form-horizontal" style="padding:15px 0">

		<div class="dhtmlxTree" id="treeboxbox_tree" style="width:250px; height:218px;overflow:auto;"></div>
		
		<div class="form-group">
		    <div class="col-sm-offset-3 col-sm-10">
		      <input type="hidden" name="chid" value="0" />
		      <button type="button" class="btn btn-success" id="workFlowSureBtn">确定</button>
		      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		      <button type="button" class="btn btn-danger"  data-dismiss="modal">取消</button>
		    </div>
		  </div>
		</form>
    </div>
  </div>
</div>

<div class="modal fade addUserByWorkFlowBox" tabindex="-1" role="dialog" aria-labelledby="edit_user_jump" id='addUserByWorkFlowBox'>
  <div class="modal-dialog modal-sm" style="width:40%">
  		
    <div class="modal-content">
		<div class="modal-header">
	        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	        <h4 class="modal-title" id="myModalLabel">添加审核人员</h4>
	    </div>
		<form action="" method="POST" class="form-horizontal" style="padding:15px 0">
		<div class="form-group">
			<label for="username" class="col-sm-3 control-label">姓名</label>
			<div class="col-sm-5" class="form-control">

				<select name="username" id="username">
					<foreach name="userList" item="val">
						<option value="{$val.uid}">{$val.realname}</option>
					</foreach>
				</select>
			</div>
		</div>

		<div class="form-group">
		    <div class="col-sm-offset-3 col-sm-10">
		      <input type="hidden" name="chid" value="0" />
		      <button type="button" class="btn btn-success" id="addUserByWorkFlowBtn">确定</button>
		      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		      <button type="button" class="btn btn-danger"  data-dismiss="modal">取消</button>
		    </div>
		  </div>
		</form>
    </div>
  </div>
</div>


<div class="contextMenu" id="myMenu1">
      <ul>
        <li id="addWorkFlow">插入审核流</li>
        <li id="delWorkFlow">删除审核流</li>
        <li id="addUserByWorkFlow">添加人员</li>
        <li id="addUser">添加人员</li>
        <li id="delUser">删除人员</li>
      </ul>
</div>
<div id="addUserBox" style="display: none;">

</div>	

<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/dhtmlx.css">
<script src="__PUBLIC__/js/dhtmlx.js" type="text/javascript"></script>
<script src="__PUBLIC__/js/jquery.contextmenu.js" type="text/javascript"></script>
<!-- <link rel="stylesheet" type="text/css" href="__PUBLIC__/css/dhtmlxmenu.css">
<script src="__PUBLIC__/js/dhtmlxmenu.js" type="text/javascript"></script> -->

<script type="text/javascript">
	$("#username").singleSelect({width:"100px"});

	$('#inputname').keypress(function(){
		var re=/^[a-z]+$/i;
		var obj=this;
		if(re.test($(obj).val())){
			$.get('{:U("ajaxGetRealName")}?pingyin='+$(obj).val(),function(data){
				if(data.status == 1){
					$(obj).parents('tr').remove();
				}
			})
		}
	})
	$('.delchannel').click(function(){
		if(confirm('是否确定删除此职位？')){
			var obj=this;
			$.get('{:U("ajaxDelChannel")}?chid='+$(obj).data('chid'),function(data){
				if(data.status == 1){
					$(obj).parents('tr').remove();
				}
			})
		}
	})

	$('#delchannelall').click(function(){
		if(confirm('是否确定删除？')){
			var id = $('[name=chid]:checked').map(function(){
				return $(this).val();
			}).get().join();

			$.post('{:U("ajaxDelChannel")}', {'chid': id},function(data){
				if(data.status == 1){
					$('[name=chid]:checked').parents('tr').remove();
				}
			})
		}
	})

	$('#newchannelbox').on('show.bs.modal', function (e) {
  		$('#newchannelbox form').get(0).reset();
	})

	$('#newchannelBtn').on('click', function () {
  		$.post("{:U('Admin/System/ajaxChannelAdd')}", $('.newchannelbox form').serialize(), function (data) {
  			if(data.status == 1) {
  				$('#newchannelbox').modal('hide');
  				window.location.reload();
  			} else {
  				alert(data.msg);
  			}
  		});
	});

	$('.editchannel').click(function(){
		$('#editchannelbox form').get(0).reset();
		$.get("{:U('Admin/System/ajaxChannelData')}?chid=" + $(this).data('chid'), function(data){
  			if(data.status == 1){
  				$("#editchannelbox input[name='name']").val(data.info['name']);
  				$("#editchannelbox input[name='cmsid']").val(data.info['cmsid']);
  				$("#editchannelbox #status_" + data.info['status']).attr("checked", true); 
  				$("#editchannelbox input[name='sort']").val(data.info['sort']);
  				$("#editchannelbox input[name='chid']").val(data.info['chid']);
  			}
  		}, 'json')
		$('#editchannelbox').modal('show');
	});


	$('#editchannelBtn').on('click', function () {
  		$.post("{:U('Admin/System/ajaxChannelEdit')}", $('.editchannelbox form').serialize(), function (data) {
  			if(data.status == 1) {
  				$('#editchannelbox').modal('hide');
  			} else {
  				alert(data.msg);
  			}
  		});
	});

	var menu, tree, itemId, isChild, maxid = 1, curid = 0, total = [], json = {};

	$('.workflow').on('click', function () {
		curid = $(this).data('chid');
		tree.deleteChildItems(0);
		tree.load('{:U('Admin/System/ajaxRelationByChannel')}?chid=' + $(this).data('chid'), 'json');
		$.get('{:U('Admin/System/ajaxRelationByChannel')}?chid=' + $(this).data('chid'), function (data) {		
			json = data;	
		}, 'json');

		$('#editworkflowbox').modal('show');
	});


	function initTree() {
		tree = new dhtmlXTreeObject("treeboxbox_tree","100%","100%",0);
		tree.setImagePath("__PUBLIC__/images/dhxtree_skyblue/");
		tree.setDataMode('json');

		tree.attachEvent("onRightClick", function(id){
			console.log(id);
			itemId = id;
			isChild = tree.hasChildren(id);
			a();
		});		
	}


	function a() {
		$('#treeboxbox_tree span').contextMenu('myMenu1', {
			bindings:  {
				'addWorkFlow': function(t) {
					var k = 0;
					$.each(json.item, function (i, n) {
						k++;
					});
					json.item.push({'id': 'T_' + (k + 1), 'text': (k + 1) + '级审核', 'child':1, 'open': 1,'item': []});
					tree.deleteChildItems(0);
					tree.parse(json, 'json');
				},
				'addUser': function(t) {
				 	$('#addUserByWorkFlowBox').modal('show');
				},
				'addUserByWorkFlow': function (t) { 
					$('#addUserByWorkFlowBox').modal('show');
				},

				'delWorkFlow': function(t) {
					var index = tree.getIndexById(itemId);
					if(index > 0){
						delete json.item[index];
					} else {
						alert('最后一层不能被删除');
					}

					var n = 0, arr = {"id":0, 'item':[]};
					$.each(json.item, function (i, t) {
						if(t) {
							t['text'] = (n + 1) + '级审核'; 
							t['id'] = 'T_' + n;
							arr.item.push(t);
							n++;
						}
					});

					json = arr;
					tree.deleteChildItems(0);
					tree.parse(arr, 'json');
				},
				'delUser': function(t) {
				 	tree.deleteItem(itemId);	
				}
			},
			onShowMenu: function (e, menu) {
				if(itemId.indexOf('T') == -1) {
					$('#delWorkFlow, #addWorkFlow, #addUserByWorkFlow', menu).remove();
				}  else {
					$('#delUser, #addUser', menu).remove();
				}

				return menu;
			}
		});	

	}

	initTree();
	a();

	$('#jqContextMenu').css('z-index', '50000');

	$('#addUserByWorkFlowBtn').on('click', function () {
		if($('#username_singleSelect_input').val()) {
			var id = $('#username_singleSelect_content .singleSelectCheckDiv_select .singleSelectCheckValue').text();
			var index = tree.getIndexById(itemId);
			console.log(json);
			json.item[index].item.push({'id': id, 'text': $('#username_singleSelect_content .singleSelectCheckDiv_select .singleSelectCheckSpan').text()});
			tree.deleteChildItems(0);
			tree.parse(json, 'json');
		}
		$('#addUserByWorkFlowBox').modal('hide');
	});

	$("#workFlowSureBtn").on('click', function() {
		var nodes = tree.serializeTreeToJSON();
		nodes = eval('(' + nodes + ')');
		$.post('{:U('workFlowEdit')}', {"postarr": nodes, 'chid' : curid}, function (data) {
			if(data.status == 1) {
				// alert(data.msg);
				$('#editworkflowbox').modal('hide');
			} else {
				alert(data.msg);
			}
		});

	});

</script>

</block>